package com.mikey.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author :Mikey
 * @description :异步调用
 * @create :2022-09-24 11:34:00
 */
public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //发起一个请求,没有返回值的
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println(Thread.currentThread().getName()+"runAsync");
//        });
//        System.out.println("1111");
//        completableFuture.get();
        CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
            System.out.println(Thread.currentThread().getName()+"runAsync");
            int i=10/0;
            return 1024;
        });
        completableFuture.whenComplete((t,u)->{
            System.out.println("t:"+t);//正常返回结果
            System.out.println("u:"+u);
        }).exceptionally((u)->{
            System.out.println(u.getMessage());
            return 233;
        }).get();
    }
}
